import { getTruthyParams } from '@/libs/common.js'

export default {
  data() {
    return {
      tableDataList: [],
      loadings: false,
      searchInfo: {},
      pagingInfo: {
        page_size: 30,
        curr_page: 1,
        total: 0,
      },
    }
  },

  computed: {
    tabelHeight() {
      return (h = 800) => (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - h
    },
  },

  methods: {
    /**
     * 删除
     */
    toVoid(data = false) {
      let msg = '确定删除已勾选信息吗？'
      if (data && data.id) {
        msg = `确定删除 ${data.name} 此信息吗？`
      }
      this.$confirm(msg, '温馨提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        try {
          let ids = []
          let res = null
          if (data && data.id) {
            ids = [data.id]
          } else {
            ids = this.selectIds
          }
          res = await this.deleteApiHandler(ids)

          console.log(res)
          if (res.code == 3) {
            this.$message({
              message: '删除成功',
              type: 'success',
            })
            this.getListDataHandler()
          } else {
            this.$message({
              message: '失败',
              type: 'warning',
            })
          }
        } catch (error) {
          this.$message({
            message: '网络异常/失败',
            type: 'warning',
          })
        }
      })
    },

    clearSearch() {
      const searchParams = getTruthyParams(this.searchInfo)
      if (Object.keys(searchParams).length > 0) {
        this.searchInfo = {}
        this.getListDataHandler()
      } else {
        this.$confirm('您还没有输入查询信息', '温馨提示', {
          confirmButtonText: '知道了',
          showCancelButton: false,
          type: 'warning',
        })
      }
    },

    sizeCurrentChange({ val, type }) {
      if (type === 'size') {
        this.pagingInfo.page_size = val
      } else {
        this.pagingInfo.curr_page = val
      }
      this.getListDataHandler()
    },
  },
}
